<template>
  <div>
    <!-- ElementUI： Dialog对话框
    -->
    <el-dialog
      :title="curid == '' ? '添加菜单' : '修改菜单'"
      :visible.sync="obj.isshow"
    >
      <el-form :model="form">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择上级菜单">
            <el-option
              :key="item.id"
              v-for="item in menuArr ? menuArr : []"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <template>
            <el-radio v-model="form.type" :label="1">目录</el-radio>
            <el-radio v-model="form.type" :label="2">菜单</el-radio>
          </template>
        </el-form-item>
        <el-form-item
          v-show="form.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              :key="ind"
              v-for="(item, ind) in iconArr"
              :label="item"
              :value="item"
            >
              <template>
                <i :class="item"></i>
              </template>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-show="form.type == 2"
          label="菜单地址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <!-- 
            active-value: switch启用时的值
            inactive-value: switch禁用时的值
          -->
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="obj.isshow = false">取 消</el-button>
        <el-button v-if="curid == ''" type="primary" @click="addmenu"
          >添加</el-button
        >
        <el-button @click="modifymenu" v-else type="primary">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMenu,
  getOneMenu,
  updateOneMenu,
  
} from "../../request/api.js";
export default {
  props: ["obj", "menuArr"],
  methods: {

    modifymenu() {
      //根据要修改的菜单id修改菜单数据
      let obj1 = {
        id: this.curid,
        ...this.form,
      };
      updateOneMenu(obj1)
        .then((d) => {
          if (d.data.code == 200) {
            //修改成功
            this.empty(); //重置form中的数据:
            this.obj.isshow = false; //关闭表单页面
            this.$emit("refreshmenu"); //刷新父组件中的菜单数据：
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    getOneMenu(id) {
      //根据要修改的菜单id查询该菜单数据
      getOneMenu({ id })
        .then((d) => {
          this.curid = id; //要修改的菜单id
          if (d.data.code == 200) {
            if (d.data.list.pid == 0) {
              d.data.list.pid = "";
            }
            this.form = d.data.list; //获取到的要修改的数据渲染到页面上
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    addmenu() {
      //添加菜单
      addMenu(this.form)
        .then((d) => {
          if (d.data.code == 200) {
            //添加成功
            this.empty(); //重置表单项
            this.obj.isshow = false; //关闭表单页面
            this.$emit("refreshmenu"); //刷新父组件中的菜单数据：
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    empty() {
      //重置表单
      this.curid = ""
      this.form = {
        pid: "", //上级菜单id
        title: "", //菜单名称
        icon: "", //菜单图标
        type: 1, //菜单类型
        url: "", //菜单地址
        status: 1, //状态： 1为启用 2为禁用
      };
    },
  },
  data() {
    return {
      curid: "", //要修改的菜单id
      formLabelWidth: "120px",
      iconArr: [
        "el-icon-setting",
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-goods",
      ],
      form: {
        pid: "", //上级菜单id
        title: "", //菜单名称
        icon: "", //菜单图标
        type: 1, //菜单类型
        url: "", //菜单地址
        status: 1, //状态： 1为启用 2为禁用
      },
    };
  },
};
</script>

<style>
</style>